'''
查询镜像中是否残留有未删除的ascend_install.info和version.info
'''
import os
import platform
from datetime import datetime as dt


class Logger():
    def __init__(self, file_name) -> None:
        self.file_name = file_name
    
    def _write(self, msg, level):
        now = dt.now().strftime("%Y-%m-%d %H:%M:%S")
        with open(self.file_name, 'a') as f:
            f.write(now + level + msg + '\n')
    
    def info(self, msg, *args):
        msg = str(msg)
        if args is not None:
            for i in args:
                msg += ' ' + str(i)
        self._write(msg, ' [INFO] ')


if __name__ == '__main__':
    check_list = [
        # 'ascendhub.huawei.com/public-ascendhub/ascend-infer-arm:21.0.1',
        # 'ascendhub.huawei.com/public-ascendhub/ascend-infer-arm:20.2.0',
        'ascendhub.huawei.com/public-ascendhub/tensorflow-modelzoo:21.0.2',
        # 'ascendhub.huawei.com/public-ascendhub/ascend-tensorflow:21.0.2-centos7.6',
        # 'ascendhub.huawei.com/public-ascendhub/ascend-tensorflow:21.0.2-ubuntu18.04',
        # 'ascendhub.huawei.com/public-ascendhub/ascend-tensorflow:5.0.1-centos7.6',
        # 'ascendhub.huawei.com/public-ascendhub/ascend-tensorflow:5.0.1-ubuntu18.04',
        # 'ascendhub.huawei.com/public-ascendhub/ascend-tensorflow:5.0.1-debian10.0-x64',
        # 'ascendhub.huawei.com/public-ascendhub/ascend-tensorflow:20.2.rc1-ubuntu18.04-arm64',
        # 'ascendhub.huawei.com/public-ascendhub/ascend-tensorflow:20.2.rc1-centos7.6-x64',
        # 'ascendhub.huawei.com/public-ascendhub/ascend-tensorflow:20.2.rc1-debian9.9-x64',
        # 'ascendhub.huawei.com/public-ascendhub/ascend-tensorflow:20.2.rc1-ubuntu18.04-x64',
        # 'ascendhub.huawei.com/public-ascendhub/ascend-tensorflow:20.1.rc1-ubuntu18.04',
        # 'ascendhub.huawei.com/public-ascendhub/ascend-infer:21.0.2-centos7.6',
        # 'ascendhub.huawei.com/public-ascendhub/ascend-infer:21.0.2-ubuntu18.04',
        # 'ascendhub.huawei.com/public-ascendhub/ascend-infer:5.0.1-ubuntu18.04',
        # 'ascendhub.huawei.com/public-ascendhub/ascend-infer:5.0.1-centos7.6',
        # 'ascendhub.huawei.com/public-ascendhub/ascend-pytorch-arm:21.0.1',
        # 'ascendhub.huawei.com/public-ascendhub/ascend-mindspore-arm:21.0.1.spc001',
        # 'ascendhub.huawei.com/public-ascendhub/ascend-mindspore:21.0.2-ubuntu18.04',
        # 'ascendhub.huawei.com/public-ascendhub/ascend-mindspore:21.0.2-centos7.6',
        #'ascendhub.huawei.com/public-ascendhub/ascend-mindspore:5.0.1-centos7.6',
        #'ascendhub.huawei.com/public-ascendhub/ascend-mindspore:5.0.1-ubuntu18.04',
        'ascendhub.huawei.com/public-ascendhub/mindspore-modelzoo:21.0.2',
        # 'ascendhub.huawei.com/public-ascendhub/infer-modelzoo:21.0.2',
        'ascendhub.huawei.com/public-ascendhub/pytorch-modelzoo:20.1.0',
        'ascendhub.huawei.com/public-ascendhub/pytorch-modelzoo:20.2.0',
        'ascendhub.huawei.com/public-ascendhub/pytorch-modelzoo:20.2.0.T101',
        'ascendhub.huawei.com/public-ascendhub/pytorch-modelzoo:21.0.1',
        'ascendhub.huawei.com/public-ascendhub/pytorch-modelzoo:21.0.2',
        # 'ascendhub.huawei.com/public-ascendhub/ascend-infer-x86:21.0.1',
        # 'ascendhub.huawei.com/public-ascendhub/ascend-infer-x86:20.2.0',
        'ascendhub.huawei.com/public-ascendhub/ascend-mindspore-x86:21.0.1.spc001',
        'ascendhub.huawei.com/public-ascendhub/ascend-mindspore-x86:21.0.1',
        'ascendhub.huawei.com/public-ascendhub/ascend-mindspore-x86:5.0.1',
        'ascendhub.huawei.com/public-ascendhub/ascend-mindspore-x86:20.2.0',
        'ascendhub.huawei.com/public-ascendhub/ascend-mindspore-x86:20.1.0',
    ]
    logger = Logger('find_info.log')
    finds = []
    arch = platform.machine()
    symbol = {'aarch64':["x64", 'x86'], "x86_64": ['arm', 'aarch64']}.get(arch.lower())
    # 登录ascend-hub
    for img in check_list:
        flag = False
        for i in symbol:
            if i in img:
                flag = True
        if flag:
            continue
        res = os.system('docker pull %s' % img)
        if res != 0:
            raise OSError("docker pull %s failed" % img)
        res = os.system("docker run -itd --rm --name test_info --user root %s /bin/bash" % img)
        if res != 0:
            raise OSError("docker run %s failed" % img)
        res = os.system("docker exec test_info ls /etc/ascend_install.info")
        if res != 0:
            logger.info('%s had no ascend_install.info' % img)
            logger.info(res)
        else:
            logger.info('%s had ascend_install.info' % img)
            logger.info(res)
            finds.append(img)
        os.system('docker stop test_info')
    for i in check_list:
        os.system('docker rmi -f %s' % i)
    print(finds)
